<?php
/**
 * Object Relational Persistence Mapping Class for table: rad_queue
 *
 * @package	com.athena
 * @author	LHV
 */
class RadQueue extends ORDataObject {

	/**#@+
	 * Fields of table: rad_queue mapped to class members
	 */
	var $rad_queue_id		= '';
	var $rad_ticket_id		= '';
	var $room_id       		= '';
	var $start_waiting		= '';
	/**#@-*/


	/**
	 * DB Table
	 */
	var $_table = 'rad_queue';

	/**
	 * Primary Key
	 */
	var $_key = 'rad_queue_id';

	/**
	 * Internal Name
	 */
	var $_internalName = 'RadQueue';

	/**
	 * Handle instantiation
	 */
	function RadQueue() {
		parent::ORDataObject();
	}

	/**
	 * Delete a radiology assignment ticket from the queue
	 *
	 * @param int $radTicketId Id of the radiology ticket
	 */
	public function deleteAssignmentTicket($radTicketId) {
		$sql    =   "DELETE FROM $this->_table WHERE rad_ticket_id=$radTicketId";
        $this->_execute($sql);
	}

	/**
	 * A wrapper of populateArray method
	 *
	 * @param int $radTicketId Id of radiology ticket
	 * @param int $roomId  Room Id
	 * @param datetime $startTime  Time at which the ticket start waiting
	 */
	public function setData($radTicketId, $roomId, $startTime) {
        $data   =   array(
            'rad_ticket_id' =>  $radTicketId,
            'room_id'       =>  $roomId,
            'start_waiting' =>  $startTime
        );
        $this->populateArray($data);
	}

	/**#@+
     * Getters
     */
    public function getRoomId() {
        return $this->room_id;
    }
    /**#@-*/

    /**
     * Get rad_queue object by rad ticket id
     *
     * @param int $radTicketId Radiology ticket id
     * @return object RadQueue object
     */
	public function getObjectByTicket($radTicketId) {
        $sql    =   "SELECT $this->_key id FROM $this->_table WHERE rad_ticket_id=$radTicketId";
        $res    =   $this->_execute($sql);
        $obj    =   Celini::newORDO('RadQueue', $res->fields['id']);
        return $obj;
	}

	public function getQueueItems($roomId, $enumPatientType, $enumTicketStatus) {
        $field =   L10nHelper::changeField(L10nHelper::getLocale(), 'name');

        $ds = & new Datasource_sql();
        $sql = array(
	        'cols' =>  "t.rad_queue_id, t.room_id, rt.rad_ticket_id, rt.patient_id, pa.patient_code, p.name patient_name,
	                t.start_waiting, TIMESTAMPDIFF(MINUTE, t.start_waiting, NOW()) waiting_time,
	                rt.treatment_department_id, dept.$field treatment_department_name",
	        'from' =>  "$this->_table t
	                INNER JOIN rad_ticket rt ON (t.rad_ticket_id = rt.rad_ticket_id)
	                INNER JOIN patient pa ON (rt.patient_id = pa.person_id)
	                INNER JOIN person p ON (pa.person_id = p.person_id)
	                LEFT JOIN department dept ON (rt.treatment_department_id = dept.department_id)
	                ",
            'where' =>  "t.room_id=$roomId AND rt.enum_patient_type = $enumPatientType AND rt.enum_ticket_status = $enumTicketStatus",
            'orderby'   =>  "t.start_waiting ASC"
        );

        $ds = & new Datasource_sql();
        $ds->setup($this->_db, $sql);
        $result = $ds->toArray();

        return $result;
	}

	public function checkItemExist($radTicketId, $enumTicketStatus) {
        $sql    =   "SELECT * FROM $this->_table t INNER JOIN rad_ticket rt ON t.rad_ticket_id=rt.rad_ticket_id
                    WHERE t.rad_ticket_id='$enumTicketStatus' AND rt.rad_ticket_id='$enumTicketStatus'";
        $res    =   $this->_execute($sql);
        if($res && !$res->EOF) {
            return true;
        }
        return false;
	}
}
?>
